package com.itx.gatewayx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itx.gatewayx.domain.News;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 新闻Mapper接口
 *
 * @author itx
 */
@Mapper
public interface NewsMapper extends BaseMapper<News> {
    
    /**
     * 分页查询新闻列表
     * 
     * @param page 分页参数
     * @param categoryId 分类ID，可选
     * @return 分页信息
     */
    @Select({
        "<script>",
        "SELECT n.*, u.nickname as author, u.avatar as author_avatar, c.name as category_name ",
        "FROM news n ",
        "LEFT JOIN user u ON n.author_id = u.id ",
        "LEFT JOIN news_category c ON n.category_id = c.id ",
        "WHERE n.del_flag = 0 AND n.status = 1 ",
        "<if test='categoryId != null and categoryId != \"\"'>",
        "AND n.category_id = #{categoryId} ",
        "</if>",
        "ORDER BY n.publish_time DESC",
        "</script>"
    })
    IPage<News> selectNewsPage(Page<News> page, @Param("categoryId") String categoryId);
    
    /**
     * 查询热门新闻
     * 
     * @param limit 条数限制
     * @return 热门新闻列表
     */
    @Select({
        "SELECT n.*, u.nickname as author, u.avatar as author_avatar, c.name as category_name ",
        "FROM news n ",
        "LEFT JOIN user u ON n.author_id = u.id ",
        "LEFT JOIN news_category c ON n.category_id = c.id ",
        "WHERE n.del_flag = 0 AND n.status = 1 ",
        "ORDER BY n.views DESC, n.likes DESC, n.comments DESC ",
        "LIMIT #{limit}"
    })
    List<News> selectHotNews(@Param("limit") int limit);
    
    /**
     * 搜索新闻
     * 
     * @param page 分页参数
     * @param keyword 关键词
     * @return 分页信息
     */
    @Select({
        "SELECT n.*, u.nickname as author, u.avatar as author_avatar, c.name as category_name ",
        "FROM news n ",
        "LEFT JOIN user u ON n.author_id = u.id ",
        "LEFT JOIN news_category c ON n.category_id = c.id ",
        "WHERE n.del_flag = 0 AND n.status = 1 ",
        "AND (n.title LIKE CONCAT('%', #{keyword}, '%') ",
        "OR n.content LIKE CONCAT('%', #{keyword}, '%') ",
        "OR n.summary LIKE CONCAT('%', #{keyword}, '%')) ",
        "ORDER BY n.publish_time DESC"
    })
    IPage<News> searchNews(Page<News> page, @Param("keyword") String keyword);
    
    /**
     * 增加浏览量
     * 
     * @param newsId 新闻ID
     * @return 影响行数
     */
    @Update("UPDATE news SET views = views + 1 WHERE uniquekey = #{newsId}")
    int incrementViews(@Param("newsId") String newsId);
    
    /**
     * 更新点赞数
     * 
     * @param newsId 新闻ID
     * @param count 数量变化(可正可负)
     * @return 影响行数
     */
    @Update("UPDATE news SET likes = likes + #{count} WHERE uniquekey = #{newsId}")
    int updateLikes(@Param("newsId") String newsId, @Param("count") int count);
    
    /**
     * 更新评论数
     * 
     * @param newsId 新闻ID
     * @param count 数量变化(可正可负)
     * @return 影响行数
     */
    @Update("UPDATE news SET comments = comments + #{count} WHERE uniquekey = #{newsId}")
    int updateComments(@Param("newsId") String newsId, @Param("count") int count);
} 